package com.lxy.demo.dao;

import com.lxy.demo.entity.EdocEntry;
import com.lxy.demo.utils.DruidManger;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class EdocEntryDaoImpl implements EdocEntryDao{
    QueryRunner qr = new QueryRunner(DruidManger.getDataSource());
    @Override
    public List<EdocEntry> findEdocEntry(String id) {
        String sql = " select e.*,c.name from edoc_category c,edoc_entry e where c.id=e.categoryId ";
        try {
            if (id != null && !id.isEmpty()){
                sql += " and c.id = ? ";
                return qr.query(sql,new BeanListHandler<EdocEntry>(EdocEntry.class),id);
            }else {
                return qr.query(sql,new BeanListHandler<EdocEntry>(EdocEntry.class));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertInfo(EdocEntry edocEntry) {
        String sql = " insert  into `edoc_entry` values(null,?,?,?,?,?) ";
        Integer categoryId = edocEntry.getCategoryId();
        Date createdate = edocEntry.getCreatedate();
        String summary = edocEntry.getSummary();
        String title = edocEntry.getTitle();
        String uploaduser = edocEntry.getUploaduser();
        try {
            return qr.update(sql,categoryId,title,summary,uploaduser,createdate);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
